/**
 * related to caseSearch.ui
 * 
 * @Author : 2037630@163.com
 * @Timestamp : 2017-04-19
 */
var notify = sm("do_Notification");
var Controller = require("core/controller");
var constant = require('config/constant');
var nf = sm("do_Notification");
var Controller = require("core/controller");
var net =require("util/net")
var server=require("config/server")
var log = require("util/log")
var storage=require("util/storage")
var navigate = require("util/navigate")
var router = require('config/router')
var App = sm('do_App');
var page=sm("do_Page");
var hisTitle=ui("hisTitle")
Controller({
	mapping : {
		"caseCustomer" : "customerData",
		"historyCustomer":"historyData",
		"search.text":"search"
	},
	ui:{
		search:"search",
		caseCustomer:"caseCustomer",
		hisTitle:"hisTitle"
	},
	data : {
//		客户搜索数据
		customerData:[],
//		要储存的历史搜索数据
		historyData:[],
//		文本框中的数据
		search:"",
//		搜索到的客户信息
		feedInfo:""
	},
	event:{
//		点击搜索按钮，跳转同时将从接口中获得的客户姓名发送到首页
		'searchButton' : {  
			'touch' : function() {
				log.info("search button")
//	点击搜索按钮跳转到首页
//	跳转同时储存历史搜索信息
				this.saveLocal(this.data.search);
				if(!storage.getItem(constant.storage.search_history)){
					hisTitle.visible=false
				}
				else{
					hisTitle.visible=true
				}
				log.info("list end");
				this.refresh("historyCustomer");
				log.info('test ned')
				this.jump(this.data.search)
			}
		},
		"caseCustomer":{
//			点击搜索页list，获取到对应索引的客户姓名，
			'touch' : function(index) {
				log.info("cus")
				log.info(index);
				log.info(this.data.customerData[index].custName)
//				点击list 同时发起搜索
				this.customerSearch(index)	
				this.saveLocal(this.data.customerData[index].custName);
				
			}
		},
		"historyCustomer":{
//			历史记录搜索事件
			"touch":function(index){
				log.info("sss")
				this.historySearch(index)
				log.info(this.data.historyData[index])
				this.saveLocal(this.data.historyData[index].value);
			}
		},
		"deleteIcon":{
			"touch":function(){
				storage.clearItem(constant.storage.search_history)
				this.data.historyData=[]
				log.info("delete")
				log.info(storage.getItem(constant.storage.search_history))
				hisTitle.visible=false;
				log.info(this.data.historyData)
				log.info("delete end")
				this.refresh("historyCustomer")
				
			}
		},
//		点击返回icon关闭当前页
		"back":{
			"touch":function(){
				App.closePage()
			}
		}
	},
	methods:{
//		将客户名发送给服务端，按照客户姓名进行匹配，之后将返回的数据通过跳转发送给首页
		customerSearch:function(i){
			log.info("list test")
			log.info(this.data.customerData[i].custName);
			this.jump(this.data.customerData[i].custName);
		},
		historySearch:function(i){
			this.jump(this.data.historyData[i].value);
		},
//		跳转到首页
		jump:function(data){
			log.info('jump')
			navigate.back(data);
		},
//		保存本地历史记录
		saveLocal:function(d){
			log.info("save seacrh")
			log.info(this.data.search)
			if(!!d){
				this.data.historyData.unshift({value:d})
			}	
			log.info(this.data.historyData)
			log.info("list test")
			this.data.historyData = this.data.historyData.slice(0,10);
			log.info(this.data.historyData );
			storage.setItem(constant.storage.search_history ,this.data.historyData);
			
		}
	},
	loaded:function(){
		log.info("开始加载")
		var saveData = storage.getItem(constant.storage.search_history);
		log.info("加载中")
		log.info(saveData)
		log.info("加载历史记录")
//      历史记录存储条件
		if(!!saveData){
			log.info(saveData)
			this.data.historyData = saveData
		}
//		是否隐藏历史搜索内容
		if(!storage.getItem(constant.storage.search_history)){
			hisTitle.visible=false
		}
		else{
			hisTitle.visible=true
		}
		this.refresh('historyCustomer')

//		客户查询数据请求
		net.send({
			server : server.organization.collectionTask,
			data : {
				cupoRectype : 70
			},
			success : function(data) {
				log.info("success loaded");
				log.info(data.content[0]);
				this.data.customerData=data.content.slice(0,10);
				log.info(this.data.customerData)
				this.refresh("caseCustomer");
			},
			error : function(data) {
				log.info(data)
			},
			scope : this
		});
//		
	}
})
